<?php

# 下单和重新下单 请添加callbackUrl 订单状态回调函数
class rrController extends AppController
{
    private $businessNo = '1556789001';

    /**
     * 下单
     */
    public function jjAction()
    {
        $rre = new RRExpressClass(TRUE);
        $timeStamp = date('Y-m-d');                # 时间 加入请求头用以校验 【非常重要】
        $startingAddress = '青羊区少城路27号';              # 发货地
        $consigneeAddress = '青羊区少城路29号';             # 收货地

        $postData = [
            'goodsName' => '测试物品',
            'goodsWeight' => 1,
            'goodsWorth' => 269,
            'startingProvince' => '四川省',
            'startingCity' => '成都市',
            'startingAddress' => $startingAddress,
            'startingPhone' => '15828444423',
            'startingName' => '发货人姓名',
            'consigneeName' => '收货人姓名',
            'consigneePhone' => '15198085649',
            'callbackUrl' => 'http://zhaipei.shjieta.com.cn/index.php?md=index&cl=rr&at=getCallback',
            'consigneeProvince' => '四川省',
            'consigneeCity' => '成都市',
            'consigneeAddress' => $consigneeAddress,
            'businessNo' => $this->businessNo, // 商家方订单号
            'serviceFees' => 0,
        ];
        $res =$rre->jj($postData,$timeStamp,$startingAddress,$consigneeAddress,10);
        echo '<pre>';
        var_dump($res);

    }


    /**
     * 下单
     */
    public function send_parameterAction()
    {
        $rre = new RRExpressClass(TRUE);
        $timeStamp = date('Y-m-d');                # 时间 加入请求头用以校验 【非常重要】
        $startingAddress = '青羊区少城路27号';              # 发货地
        $consigneeAddress = '青羊区少城路29号';             # 收货地

        $postData = [
            'goodsName' => '测试物品',
            'goodsWeight' => 1,
            'goodsWorth' => 269,
            'startingProvince' => '四川省',
            'startingCity' => '成都市',
            'startingAddress' => $startingAddress,
            'startingPhone' => '15828444423',
            'startingName' => '发货人姓名',
            'consigneeName' => '收货人姓名',
            'consigneePhone' => '15198085649',
            'callbackUrl' => 'http://zhaipei.shjieta.com.cn/index.php?md=index&cl=rr&at=getCallback',
            'consigneeProvince' => '四川省',
            'consigneeCity' => '成都市',
            'consigneeAddress' => $consigneeAddress,
            'businessNo' => $this->businessNo, // 商家方订单号
            'serviceFees' => 0,
        ];
        $res =$rre->send_parameter($postData,$timeStamp,$startingAddress,$consigneeAddress,10);
        echo '<pre>';
        var_dump($res);
        if (!empty ($res)) {
            if (!$res ['status']) {
                echo '下单失败,原因：' . $res ['msg'];
                exit ();
            }
            $this->businessNo = $res ['businessNo'];
            echo '下单成功，订单号：' . $res ['orderNo'], '第三方订单号：'.$res ['businessNo'].'，价格：' . $res ['price'];

        }
    }

    /**
     * 取消订单
     *  $businessNo  订单号
     */
    public function cancelAction()
    {
        $rre = new RRExpressClass(TRUE);
        $timeStamp = date('Y-m-d');
        $businessNo = $this->businessNo;
        $reason = '下错';     # 取消原有
        $res = $rre->cancel($timeStamp, $businessNo, $reason,10);
        if (!empty ($res)) {
            if (!$res ['status']) {
                echo '取消失败,错误代码：' . $res ['code'].'人人快递单号：'.$res ['orderNo'].'';
                exit ();
            }
            echo '订单取消成功，订单号：' . $res ['businessNo'], '';
        }
    }

    /**
     * 查询订单
     *  $businessNo  订单号
     */
    public function queryAction()
    {
        $rre = new RRExpressClass(TRUE);
        $timeStamp = date('Y-m-d');
        $businessNo = $this->businessNo;
        $res = $rre->query($timeStamp, $businessNo,10);
        if (!empty ($res)) {
            if (!$res ['status']) {
                echo '查询失败,错误代码：' . $res ['code'].'人人快递单号：'.$res ['orderNo'].'';
                exit ();
            }
            echo '以下为订单详情，第三方订单号：' . $res ['businessNo'], ''.var_dump($res);
        }
    }


    /**
     * 从新订单
     *  $businessNo  订单号
     */
    public function agingAction()
    {
        $rre = new RRExpressClass(TRUE);
        $startingAddress = '青羊区少城路27号';              # 发货地
        $consigneeAddress = '青羊区少城路29号';             # 收货地
        $agingPostData = [
            'goodsName' => '测试物品',
            'goodsWeight' => 1,
            'goodsWorth' => 10,
            'startingLng' => 104.05759,
            'startingLat' => 30.62921,
            'consigneeLng' => 104.065886,
            'consigneeLat' => 30.640848,
            'mapFrom' => 1,
            'startingProvince' => '四川省',
            'startingCity' => '成都市',
            'startingAddress' => $startingAddress,
            'startingPhone' => '15828444423',
            'startingName' => '发货人姓名',
            'consigneeName' => '收货人姓名',
            'consigneePhone' => '15198085649',
            'callbackUrl' => 'http://xxxx.com',
            'consigneeProvince' => '四川省',
            'consigneeCity' => '成都市',
            'consigneeAddress' => $consigneeAddress,
            'businessNo' => time(), // 商家方订单号
            'dispatchers' => '',
            'payType' => 4,//支付方式
            'serviceFees' => 0,
            'remark' => '这是备注',
            'version' => 2.0
        ];
        $timeStamp = date('Y-m-d');
        $businessNo = $this->businessNo;
        $res = $rre->aging($timeStamp,$agingPostData,$startingAddress,$consigneeAddress,$businessNo,10);
        if (!empty ($res)) {
            if (!$res ['status']) {
                echo '重新下单是吧,错误代码：' . $res ['code'].'人人快递单号：'.$res ['orderNo'].''.var_dump($res);
                exit ();
            }
            echo '重新下单成功，第三方订单号：' . $res ['businessNo'], ''.var_dump($res);
        }
    }


    /**
     * 获取订单配送人信息 测试中无人接单 错误代码20029
     */
    public function getorderdelivererAction()
    {
        $rre = new RRExpressClass(TRUE);
        $timeStamp = date('Y-m-d');
        $businessNo = $this->businessNo;
        $res = $rre->getorderdeliverer($timeStamp, $businessNo,10);
        if (!empty ($res)) {
            if (!$res ['status']) {
                echo '获取失败,错误代码：' . $res ['code'].'人人快递单号：'.$res ['orderNo'].'';
                exit ();
            }
            echo '以下为订单配送人详情，第三方订单号：' . $res ['businessNo'], ''.var_dump($res);
        }
    }


    /**
     * 查询余额
     */
    public function blanceAction()
    {
        $rre = new RRExpressClass(TRUE);
        $timeStamp = date('Y-m-d');
        $res = $rre->blance($timeStamp,10);
        if (!empty ($res)) {
            if (!$res ['status']) {
                echo '获取失败,错误代码：' . $res ['code'].'人人快递单号：'.$res ['orderNo'].'';
                exit ();
            }
            echo '当前余额为：' . $res ['data'], ''.var_dump($res);
        }
    }


    /**
     * 查询订单轨迹 测试中 code = -1
     */
    public function trailAction()
    {
        $rre = new RRExpressClass(TRUE);
        $timeStamp = date('Y-m-d');
        $businessNo = $this->businessNo;
        $res = $rre->trail($timeStamp,$businessNo,10);
        if (!empty ($res)) {
            if (!$res ['status']) {
                var_dump($res);
                echo '获取失败,错误代码：' . $res ['code'].'配送人姓名：'.$res ['delivererName'].'';
                exit ();
            }
            echo '配送人姓名：' . $res ['delivererName'], ''.var_dump($res);
        }
    }


    /**
     * 模拟测试任意一个订单回调
     */
    public function testCallbackAction()
    {
        $rre = new RRExpressClass(TRUE);
        $timeStamp = date('Y-m-d');
        $businessNo = $this->businessNo;
        $orderStatus = '1';                          # 需要获取的订单状态,值为【1-已接单,2-已取消,6-已签收,7-已取货，9-超时自动取消】,不传即为订单真实状态下的回调
        $operationType = '';                        # 配合orderStatus为2时生效，可用于更多的模拟状态：1-人人快递协助接单，2-人人快递协助取件，3-人人快递协助签收，5-人人快递取消接单
        $res = $rre->testCallback($timeStamp,$businessNo,$orderStatus,$operationType,10);
        if (!empty ($res)) {
            if (!$res ['status']) {
                var_dump($res);
                echo '获取失败,错误代码：' . $res ['code'].'';
                exit ();
            }
            echo '配送人姓名：' . $res ['msg'], ''.var_dump($res);
        }
    }


    /**
     * 正式回调
     * 	公共参数，
        1 ：接单，2 ： 取件，
        3 ：签收，4 ：超时：一段时间内无人接单，系统自动取消订单，
        5：客服取消订单，6：客服取消接单，
        7：客服协助接单，8：客服协助取件，
        9：客服协助签收
     */
    public function getCallbackAction(){
        $body = file_get_contents('php://input');
        file_put_contents('logs/RRgetcallback',"$body".PHP_EOL,FILE_APPEND);
        $returnInfo = json_decode($body,true);
        if($returnInfo['msgType'] == 1 || $returnInfo['msgType'] == 2 || $returnInfo['msgType'] == 3 || $returnInfo['msgType'] == 4 ||
            $returnInfo['msgType'] == 5 || $returnInfo['msgType'] == 6 || $returnInfo['msgType'] == 7 || $returnInfo['msgType'] == 8 || $returnInfo['msgType'] == 9){
            echo 'success';
        }
    }
}
